package org.ala.clickhouse.config;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.ala.tiktools.tools.EnvironmentTools;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

/**
 *	配置信息
 *
 *	@author ala
 *	@date 2025年6月7日
 */
public class ClickHouseConfig {
	
	
	public static final String 
						DEFAULT_PREFIX = "clickhouse",
						DEFAULT = "default";

	
	/**
	 * 	配置前缀
	 */
	protected String prefix;
	/**
	 * 	默认连接配置
	 */
	protected Info def;
	/**
	 * 	连接配置
	 */
	protected Map<String, Info> infos;
	
	/**
	 * 	解析配置
	 */
	public static ClickHouseConfig build(String prefix, Environment env) {
		String prefixStr = prefix == null ? DEFAULT_PREFIX : prefix;
		
		Set<String> items = EnvironmentTools.items(prefix + ".", (ConfigurableEnvironment) env);
		Map<String, Info> infos = items.stream().collect(Collectors.toMap(n -> n, n -> parseInfo(prefixStr, env, n), (o,n) -> n));
		
		ClickHouseConfig conf = new ClickHouseConfig();
		conf.setInfos(infos);
		conf.setDef(infos.get(DEFAULT));
		infos.remove(DEFAULT);
		return conf;
	}
	/**
	 * 	解析配置项
	 */
	protected static Info parseInfo(String prefix, Environment env, String name) {
		prefix = prefix + "." + name;
		
		Info info = new Info();
		info.setName(name);
		info.setUrl(env.getProperty(prefix + ".url", String.class));
		info.setUsername(env.getProperty(prefix + ".username", String.class));
		info.setPassword(env.getProperty(prefix + ".password", String.class));
		
		Pool pool = new Pool();
		info.setPool(pool);
		pool.setIdle(env.getProperty(prefix + ".pool.idle", Integer.class, 1));
		pool.setMax(env.getProperty(prefix + ".pool.max", Integer.class, 1));
		return info;
	}
	
	
	public Info getDef() {
		return def;
	}
	public void setDef(Info def) {
		this.def = def;
	}
	public Map<String, Info> getInfos() {
		return infos;
	}
	public void setInfos(Map<String, Info> infos) {
		this.infos = infos;
	}





	/**
	 * 	数据源配置信息
	 */
	public static class Info {
		/**
		 * 	数据源名称
		 */
		protected String name;
		/**
		 * 	url
		 */
		protected String url;
		/**
		 * 	用户名密码
		 */
		protected String username, password;
		/**
		 * 	连接池配置
		 */
		protected Pool pool;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
		public String getUsername() {
			return username;
		}
		public void setUsername(String username) {
			this.username = username;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		public Pool getPool() {
			return pool;
		}
		public void setPool(Pool pool) {
			this.pool = pool;
		}
	}
	/**
	 * 	连接池配置
	 */
	public static class Pool {
		/**
		 * 	空闲连接数
		 */
		protected int idle;
		/**
		 * 	最大连接数
		 */
		protected int max;
		public int getIdle() {
			return idle;
		}
		public void setIdle(int idle) {
			this.idle = idle;
		}
		public int getMax() {
			return max;
		}
		public void setMax(int max) {
			this.max = max;
		}
	}
}
